$(function(){
    //初始化树
    debugger
    typeTreeUtil.initTree();
    //初始化表
    stageTableUtil.initTable('');
});
//树操作
var typeTreeUtil = {
    initTree:function(){
        $.ajax({
            url: '/projectsteptype/selectTree',
            type: 'GET',
            dataType: 'json',
            async: false,
            success: function (result) {
                $('#typeTree').treeview({
                    highlightSelected: true,//当选择节点时是否高亮显示。
                    showBorder: true,//是否在节点上显示边框。
                    showImage: true,
                    showIcon:true,
                    levels:1,
                    data: result.data,
                    onNodeSelected: function (event, data) {
                        if(data.tag == "type"){
                            typeTreeUtil.refreshTable(data.id);
                        }
                    }
                });
            }
        });
    },
    refreshTable:function(id){
        $("#stageTable").bootstrapTable("refresh",{query: {id:id}});
    }
}
//表操作
var stageTableUtil = {
    initTable:function(id){
        $("#stageTable").bootstrapTable({
            url:'/projectsteptype/selectTable',
            method: "GET",
            toolbar:"#toolbar",
            cache: false,
            striped: true,           //表格显示条纹
            pagination: true,        //在底部显示分页组件
            pageList: [10, 20],       //设置页面可以显示的数据条数
            pageSize: 10,             // 页面数据条数
            pageNumber: 1,            // 首页页码
            sidePagination: 'server', // 设置为服务器端分页
            showColumns:true,        //是否显示内容列下拉框
            showToggle:false,         //切换详细视图和列表视图
            queryParamsType: '',
            queryParams:function (params){   //请求服务器数据时，添加额外参数
                return {
                    pageSize:params.pageSize, // 每页要显示的数据条数
                    pageNum:params.pageNumber   // 页码
                }
            },
            idField:'id',             //指定主键列
            columns:[{
                checkbox: true,   //显示一个勾选框
                align: 'center'   //居中显示
            },{
                field:'name',
                title:'阶段名称',
                align:'center',
                valign: 'middle'
            },
                {
                    field:'doc_Type',
                    title:'文件类型',
                    align:'center',
                    valign: 'middle'
                },{
                    field:'remark',
                    title:'备注',
                    align:'center',
                    valign: 'middle'
                },{
                    field:'id',
                    title:'操作',
                    width:80,
                    align:'center',
                    valign: 'middle',
                    formatter:function(value, row, index){
                        var id = value;
                        var name = row.name;
                        var doc_Type = row.doc_Type;
                        var remark = row.remark;
                        var result = "";
                        result += "<a href='javascript:;' class='btn btn-xs blue' onclick=\"stageTableUtil.edit('" + id + "','"+name+"','"+doc_Type+"','"+remark+"')\" title='编辑'><span class='glyphicon glyphicon-pencil'></span></a>";
                        result += "<a href='javascript:;' class='btn btn-xs red' onclick=\"stageTableUtil.delete('" + id + "')\" title='删除'><span class='glyphicon glyphicon-remove'></span></a>";
                        return result;
                    }
                }
            ]
        });
    },
    add:function(){
        stageTableUtil.formatForm();//格式化form表单
        var select = $("#typeTree").treeview("getSelected");
        if(select.length == 1 ){
            $("#myModalAdd").modal('show');
        }else {
            layer.msg("请先选择项目分类！");
            return;
        }

    },
    delete:function (id) {
        $.ajax({
            url: '/pro/stage/deleteStage?id='+id,
            type: 'DELETE',
            dataType: 'json',
            async:false,
            success: function (data) {
                if(data.success == true){
                    layer.msg("删除成功");
                    stageTableUtil.search();
                }else{
                    layer.msg(data.message);
                }
            },
            error: function(e,i,r){
                layer.msg('ajax加载失败');
            }
        });
    },
    edit:function (id,name,doc_Type,remark) {
        stageTableUtil.formatForm();//格式化form表单
        $("#id").val(id);
        $("#stageName").val(name);
        $("#remark").val(remark);
        $('#doc_Type').selectpicker('val',doc_Type.split(",") );
        $('#doc_Type').selectpicker('refresh');//刷新下拉框
        $("#myModalAdd").modal("show");
    },
    search:function () {
        var name = $("#name").val();
        var select = $("#typeTree").treeview("getSelected");
        var id = '';
        if(select.length > 0){
            id = select[0].id;
        }
        $("#stageTable").bootstrapTable("refresh",{query: {id:id,name:name}});
    },
    initToolbar:function () {

    },
    save:function(){
        if($("#myFormAdd").valid()){
            var select = $("#typeTree").treeview("getSelected");
            var obj = {
                id:$("#id").val(),
                name:$("#stageName").val(),
                doc_Type:$("#doc_Type").val(),
                remark:$("#remark").val(),
                stepTypeId: select[0].id

            }
            $.ajax({
                type:'post',
                url:'/pro/stage/insertStage',
                data:JSON.stringify(obj),
                async:false,
                contentType:'application/json',
                success:function(result){
                    if(result.success){
                        $("#myModalAdd").modal("hide");
                        $("#stageTable").bootstrapTable("refresh");
                        layer.msg("保存成功");
                    }
                },error:function(){
                    layer.msg("ajax请求失败！");
                }
            });
        }
    },
    formatForm:function(){
        $('#doc_Type').selectpicker('val','' );//清空下拉框
        $('form[id=myFormAdd] label.error').remove();//清空验证信息
        //清空表单
        $(':input','#myFormAdd')
            .not(':button, :submit, :reset')
            .val('')
            .removeAttr('checked')
            .removeAttr('selected');
    }
}
